#encoding=utf-8
'''
Created on 2015-03-30

@author: cooler
'''
from flask import Blueprint
from flask import  request,  abort, session
from flask import g
from service.models import *
import copy
import logging

logger = logging.getLogger('user')
logger.setLevel(logging.INFO)

user = Blueprint('user', __name__)

WHITELIST  = ['/user/login','/user/auth','/user/logout','/user/register','/user/add_user']

@user.before_request
def before_request():
    g.HEADERS =  request.headers
    g.args = request.args if request.method == 'GET' else request.form
    logger.info("request.path:"+request.path+",g.args:"+str(g.args)+",g.body:"+str(request.json))
    g.results = {
        "status":200,
        "msg":"success",
        "datas":{
            "code":1
        },
    }
    if request.path in WHITELIST:
        pass
    else:
        userinfo = session.get('userinfo')
        if userinfo:
            g.user_info = User_info.objects(username=userinfo['account']).first()
        else:
            return render_template('404.html'), 404
 
                  
@user.teardown_request
def teardown_request(exception):
    # g.session.close()
    pass

@user.errorhandler(404)
def page_not_found(error):
    return render_template('404.html'), 404


from . import user_auth
from . import account